package yannan.voiplock4ble.task;

import yannan.voiplock4ble.bean.CommonResponse;
import yannan.voiplock4ble.bean.LockBean;
import yannan.voiplock4ble.net.NetUtils;
import yannan.voiplock4ble.net.base.NetworkCallBack;

/**
 * 说明：上传锁信息
 * 作者：mainTel
 * 时间：2016/12/7 15:01
 * 备注：
 */
public class UpLoadLockInfoTask extends Thread {

    LockBean lockBean;

    public static UpLoadLockInfoTask create(LockBean lockBean) {
        UpLoadLockInfoTask loadLockInfoTask = new UpLoadLockInfoTask();
        loadLockInfoTask.lockBean = lockBean;
        return loadLockInfoTask;
    }

    @Override
    public void run() {
        upLoadLockInfo(0);
    }

    private void upLoadLockInfo(final int i) {
        if (i < 5) {
            NetUtils.getNetworkService().uploadLockState(lockBean.lockserialId, lockBean.veinKeyState + "",
                    lockBean.pwdKeyState + "", lockBean.mac, lockBean.batteryQuantity + "")
                    .enqueue(new NetworkCallBack<CommonResponse>() {
                                 @Override
                                 public void onSuc(CommonResponse resBody) {
                                     try {
                                         if (resBody.returnValue != null || !resBody.returnValue.equals("1")) {

                                         } else {
                                             int ii = i;
                                             upLoadLockInfo(++ii);
                                         }
                                     } catch (Exception e) {
                                         e.printStackTrace();
                                         int ii = i;
                                         upLoadLockInfo(++ii);
                                     }
                                 }

                                 @Override
                                 public void onFail(String msg) {
                                     int ii = i;
                                     upLoadLockInfo(++ii);
                                 }
                             }
                    );
        }

    }
}
